<template>
  <!-- 分组设备 -->
  <el-dialog title="待分组设备列表" :visible.sync="visible" width="1000px" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="模糊检索" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="设备编码/设备名称/IMEI/ICCID"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安装路段" prop="roadSectionId">
        <el-select v-model="queryParams.roadSectionId" placeholder="请选择" style="width: 100%">
          <el-option
            v-for="item in roadSectionList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table @row-click="clickRow" ref="table" :data="deviceList" @selection-change="handleSelectionChange" height="350px">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column type="index" label="序号" width="50"/>
        <el-table-column label="设备编码" align="center" prop="deviceCode" width="180"  />
        <el-table-column label="设备名称" align="center" prop="deviceName" :show-overflow-tooltip="true"  />
        <el-table-column label="ICCID" align="center" prop="iccid" width="150" />
        <el-table-column label="IMEI" align="center" prop="imei" width="150" />
        <el-table-column label="设备类型" align="center" prop="deviceType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.device_type" :value="scope.row.deviceType"/>
          </template>
        </el-table-column>
        <el-table-column label="安装路段" align="center" prop="roadSection" width="100" />
        <el-table-column label="负责人" align="center" prop="leader" width="100" />
        <el-table-column label="安装位置" align="center" prop="location" width="100" />
        <el-table-column label="安装时间" align="center" prop="installDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.installTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="设备状态" align="center" prop="deviceStatus">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.device_status" :value="scope.row.deviceStatus"/>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectUser">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getNoGroupDeviceList,groupDeviceSelectAll} from "@/api/business/device/deviceManage"
import { getDeploymentLocationByClassLevel } from "@/api/business/device/deploymentLocation"
export default {
  name: "SelectDevice",
  dicts: ['sys_normal_disable','device_type','device_status','device_type'],
  props: {
    // 设备分组
    groupId: {
      type: [Number, String]
    }
  },
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      deviceIds: [],
      // 总条数
      total: 0,
      // 未分组设备数据
      deviceList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: undefined,
        searchValue: undefined,
      },
      roadSectionList:[],//路段集合
    }
  },
  created() {
    this.getRoadSection()//加载路段
  },
  methods: {
    // 显示弹框
    show() {
      this.queryParams.groupId = this.groupId
      this.getList()
      this.visible = true
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row)
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.deviceIds = selection.map(item => item.id)
    },
    // 查询表数据
    getList() {
      this.deviceList=[]
      getNoGroupDeviceList(this.queryParams).then(res => {
        this.deviceList = res.rows
        this.total = res.total
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 选择分组设备操作 */
    handleSelectUser() {
      const groupId = this.queryParams.groupId
      const deviceIds = this.deviceIds.join(",")
      if (deviceIds == "") {
        this.$modal.msgError("请选择要分配的设备")
        return
      }
      groupDeviceSelectAll({ groupId: groupId, deviceIds: deviceIds }).then(res => {
        this.$modal.msgSuccess(res.msg)
        this.visible = false
        this.$emit("ok")
      })
    },
    /** 查询路段 */
    getRoadSection() {
      this.roadSectionList=[]
      getDeploymentLocationByClassLevel("0").then(response => {
        this.roadSectionList = response.data
      })
    },
  }
}
</script>
